package com.example.merchant.service;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.example.merchant.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.CopyOnWriteArrayList;

@Service
public class BusinessService {

// 依赖注入（控制反转）   spring 帮你 注入 依赖

    @Resource
    private UserService userService;//筛选不同用户

    @Resource(name = "GRAPE")
    private CreateCommodityService createCommodityService;

    @Resource
    private CouponService couponService;

    @Resource
    private GenerateOrdersService generateOrdersService;

    /**
     * 模拟服务主进程，允许整个项目
     */
    public void exe() {

        // TODO 主进程仅能够调用各个服务 xxxService，然后输出信息

        // 引入一个hutool

        /*
        json
        toString();
         */

        // TODO 重写 Customer toString()


        // 输出用户列表
        List<Customer> customer18YearsOld = userService.customer18YearsOld();
        System.out.println(new JSONArray(customer18YearsOld));


        List<Customer> manCustomer = userService.manCustomer();
        System.out.println(manCustomer);

        // TODO 商人创建商品
        Merchant merchant = MockDatabase.merchant();

        //创建葡萄

        createCommodityService.creatCommodity(merchant);//葡萄
        System.out.println(createCommodityService.creatCommodity(merchant));

        //创建优惠券
        //couponService.issuedEverydayCoupon();
        for (Coupon c : couponService.issuedEverydayCoupon()) {
            System.out.println("优惠券名称：" +c.getName()+ "优惠券ID"+c.getId() + "编码：" + c.getCoding());
        }
        //couponService.issuedFullDiscountCoupon();
        for (Coupon c : couponService.issuedFullDiscountCoupon()) {
            System.out.println("优惠券名称：" +c.getName()+ "优惠券ID"+ c.getId() + "编码：" + c.getCoding());
        }

        // TODO 顾客购买商品（需要校验钱够不够）
        Customer customer = new Customer();
        List<Commodity> commodityList= new ArrayList<>();
        commodityList.add(createCommodityService.creatCommodity(merchant));

        generateOrdersService.createOrder(customer18YearsOld.get(1),commodityList);
        generateOrdersService.payOrder(generateOrdersService.createOrder(customer18YearsOld.get(1),commodityList),couponService.issuedEverydayCoupon().get(1));

        /*List<Customer> customers =MockDatabase.customerList();
        for (Customer customer:customers) {
            Random r = new Random(1);
            //System.out.println(customer.getMoney());
            //System.out.println(commodity.getMoney());
            if (customer.getMoney() < commodity.getMoney()) {
                System.out.println(customer.getName() + "用户余额不足");
                continue;
            }
            // TODO 生成订单，打印输出
            if (customer.getMoney() >= commodity.getMoney()){
                int m =r.nextInt(10);
                Order order = new Order(m, commodity.getMoney(), customer.getId(),customer.getName());
                System.out.println("订单ID："+order.getId()+"\t\t"+"订单金额："+order.getMoney()+"\t\t"+"下单人："+order.getCustomerName());

            }

        }*/
//        GenerateOrdersService generateOrdersService = new GenerateOrdersService();
//        generateOrdersService.orderArrayList();
//        for (Order order : generateOrdersService.orderArrayList()) {
//            System.out.println("订单ID：" + order.getId() + "\t\t" + "订单金额：" + order.getMoney() + "\t\t" + "下单人：" + order.getCustomerName());
//        }

    }

    // TODO 不要随意定义构造方法






}
